﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using FacebookApp;
using System.Configuration;

public partial class Modules_Menu : System.Web.UI.UserControl
{
    StringBuilder sb = new StringBuilder();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //string headerpath = ConfigurationSettings.AppSettings.GetValues("fb_headerpath")[0].ToString();
            List<Category> ct = new List<Category>();
            ct = CategoryService.Category_GetByTop("", "", " [Order] ");
            sb.Append("\n<li><a href=" + "/Default.aspx" + ">" + "Trang chủ" + "</a></li>");
            sb.Append("\n<li><a style=\"cursor: pointer;\">" + "Danh mục" + "</a>");
            sb.Append("\n<ul>");
            for (int i = 0; i < ct.Count; i++)
            {
                sb.Append("\n<li><a href=" + "/Pages/ListGroupProduct.aspx?Id=" + ct[i].Id.ToString() + "&GroupName="+ct[i].Name.Replace(" ","_")+">" + ct[i].Name + "</a>");
            }
            sb.Append("\n</ul>\n</li>");
            sb.Append("\n<li><a href=" + "/Pages/Order.aspx" + ">" + "Giỏ hàng" + "</a></li>");
            sb.Append("\n<li><a href=" + "/Pages/Contact.aspx" + ">" + "Liên hệ" + "</a></li>");
            ltrMenu.Text = sb.ToString();

            if (Session["WB_UserId"] == null)
            {
                pnLogin.Visible = true;
                pnLogout.Visible = false;
            }
            else
            {
                lbUsername.Text = Session["WB_FullName"].ToString() + " | ";
                pnLogin.Visible = false;
                pnLogout.Visible = true;
            }
    //        function show_hide() {

    //$(".bodycss").slideToggle("slow"); }
            //StringBuilder sb2 = new StringBuilder();
            //sb2.Append("<script type='text/javascript' src='/Scripts/Show.js'></script>");
            //if (!Page.ClientScript.IsStartupScriptRegistered(typeof(Page), "loaddata"))
            //{
            //    Page.ClientScript.RegisterStartupScript(typeof(Page), "loaddata", sb2.ToString());
            //}
            
        }
    }

    protected void lbtLogin_Click(object sender, EventArgs e)
    {
        //if (Session["mdAccount"] != null)
        //    (Session["mdAccount"] as Panel).Visible = !(Session["mdAccount"] as Panel).Visible;
        //WebMsgBox.Show((Session["mdAccount"] as Panel).Visible.ToString());
        if (txtusername.Text.Equals(""))
        {
           // WebMsgBox.Show("Nhập tên đăng nhập.");
            imWarningusername.Visible = true;
            txtusername.Focus();
            return;
        }
        else
            imWarningusername.Visible = false;
        if (txtpassword.Text.Equals(""))
        {
            //WebMsgBox.Show("Nhập mật khẩu.");
            imWarningpass.Visible = true;
            txtpassword.Focus();
            return;
        }
        else
            imWarningpass.Visible = false;

        User u = UserService.User_CheckLogin(txtusername.Text, FunctionClass.encryptData(txtpassword.Text));
        if (u != null)
        {
            imWarningwrong.Visible = false;
            Session["WB_UserId"] = u.Id;
            Session["WB_GroupUser_Id"] = u.GroupUser_Id;
            //Session["WB_UserName"] = u.UserName;
            Session["WB_FullName"] = u.FullName;
            lbUsername.Text = u.FullName + " | ";
            pnLogin.Visible = false;
            pnLogout.Visible = true;
            Response.Redirect(Request.Url.AbsoluteUri);
            //StringBuilder sb2 = new StringBuilder();
            //sb2.Append("<script type='text/javascript' src='/Scripts/Show.js'></script>");
            //if (!Page.ClientScript.IsStartupScriptRegistered(typeof(Page), "loaddata"))
            //{
            //    Page.ClientScript.RegisterStartupScript(typeof(Page), "loaddata", sb2.ToString());
            //}
        }
        else
        {
           // WebMsgBox.Show("Kiểm tra lại tên đăng nhập/ mật khẩu!");
            imWarningwrong.Visible = true;
            txtpassword.Focus();
        }
    }

    protected void lbtLogout_Click(object sender, EventArgs e)
    {
        Session["WB_UserId"] = null;
        Session["WB_GroupUser_Id"] = null;
        //Session["WB_UserName"] = null;
        Session["WB_FullName"] = null;

        txtusername.Text = "";
        pnLogin.Visible = true;
        pnLogout.Visible = false;
        Response.Redirect(Request.Url.AbsoluteUri);
    }

    protected void lbtregister_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Pages/RegisterAccount.aspx");
    }
    protected void lbforgotpassword_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Pages/ForgotPassword.aspx");
    }
}